import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/view/component/UserIconView.dart';
import 'package:scroll_to_index/scroll_to_index.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/ImConstant.dart';
import '../../../bloc/SelectGroupUsersCubit.dart';
import '../../../bloc/SelectUsersCubit.dart';
import '../../ListSeparator.dart';

class GroupUserItemSelectView extends StatelessWidget {
  final ImUser user;
  final ImUser? preUser;
  final AutoScrollController? selectedUserScrollController;
  final bool? isSelected;

  const GroupUserItemSelectView({
    required this.user,
    this.preUser,
    this.selectedUserScrollController,
    this.isSelected,
    super.key
  });

  @override
  Widget build(BuildContext context) {
    if (preUser == null || (preUser!.firstLetter ?? '#') != user?.firstLetter) {
      return Column(
        children: [
          _buildGroupTitle(),
          _buildUser(context),
          ListSeparator(indent: 10,),
        ],
      );
    }
    return Column(
      children: [
        _buildUser(context),
        ListSeparator(indent: 10,),
      ],
    );
  }

  Widget _buildUser(BuildContext context) {
    var selectGroupUsersCubit = context.read<SelectGroupUsersCubit>();
    return InkWell(
      onTap: isSelected??false ? null : () {
        if (selectGroupUsersCubit.state.selectedUserMap.containsKey(user.userId)) {
          selectGroupUsersCubit.add(SelectGroupUsersRemoveUserEvent(user));
        } else {
          selectGroupUsersCubit.add(SelectGroupUsersAddUserEvent(user));
        }
      },
      child: Material(
        color: isSelected??false ? Colors.grey : (selectGroupUsersCubit.state.selectedUserMap.containsKey(user.userId) ?
          ImConstant.IM_MAIN_BACKGROUND_COLOR : ImConstant.IM_RESERVE_BACKGROUND_COLOR),
        child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 10, right: 10),
            child: Radio(
              value: user.userId,
              groupValue: isSelected??false ? user.userId : selectGroupUsersCubit.state.selectedUserMap[user.userId]?.userId,
              onChanged: (String? value) {
                if (selectGroupUsersCubit.state.selectedUserMap.containsKey(user.userId)) {
                  selectGroupUsersCubit.add(SelectGroupUsersRemoveUserEvent(user));
                } else {
                  selectGroupUsersCubit.add(SelectGroupUsersAddUserEvent(user));
                  selectedUserScrollController?.scrollToIndex(selectGroupUsersCubit.state.selectedUsers.length);
                }
              },
            ),
          ),
          UserIconView(user: user, iconSize: 40,),
          Padding(
            padding:  const EdgeInsets.all(10),
            child: Text(user.userName ?? '',
                style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE, fontWeight: FontWeight.w300)),
          )
        ],
      ), ),
    );
  }

  Widget _buildGroupTitle() {
    return Container(
      color: ImConstant.IM_LIST_GROUP_COLOR,
      padding: const EdgeInsets.only(left: 10),
      child: Row(
        children: [
          Text(
            (user.firstLetter ?? "#").toUpperCase(),
            style: const TextStyle(fontSize: 16),
          )
        ],
      ),
    );
  }
}
